package com.arron.algorithm.leetcodetop100.单调栈;


import java.util.Stack;

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

public class 最小栈 {

    Stack<Integer> minStack;
    Stack<Integer> stack;


    public void MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        //最小栈存最小值，将比自己大的都删除

        if (minStack.isEmpty() || minStack.peek() >= val){
            minStack.push(val);
        }

        stack.push(val);

    }

    public void pop() {
        if (!minStack.isEmpty() && stack.pop().equals(minStack.peek())){
            minStack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }

    public static void main(String[] args) {

        // 512],[-1024],[-1024],[512

        最小栈 minS = new 最小栈();
        minS.MinStack();

        minS.push(2147483646);
        minS.push(2147483646);
        minS.push(2147483647);

        minS.top();
        minS.pop();
        minS.getMin();
        //"pop","getMin","pop"
        minS.pop();
        minS.getMin();
        minS.pop();
        minS.push(2147483647);
        //"top","getMin"
        minS.top();
        minS.getMin();
        minS.push(-2147483648);

        //"top","getMin","pop","getMin"
        minS.top();
        minS.getMin();
        minS.pop();
        minS.getMin();

    }
}



